package Demo01;

/**
 * 张三吃醋要蒜  李四吃蒜要醋
 */
public class Test1 {
    Object o1 = new Object();
    Object o2 = new Object();
    public static void main(String[] args) {
        Object o1 = new Object();
        Object o2 = new Object();
            Thread thread1 = new Thread(() -> {
                synchronized (o1) {
                    System.out.println("张三吃醋...");
                    synchronized (o2) {
                        System.out.println("张三要吃醋的同时吃蒜...");
                    }
                }
            });
            Thread thread2 = new Thread(() -> {
                synchronized (o2) {
                    System.out.println("李四吃蒜...");
                    synchronized (o1) {
                        System.out.println("李四要吃蒜的同时吃醋...");
                    }
                }
            });

            thread1.start();
            thread2.start();
    }
    public void Thread1() {
        Thread thread = new Thread(() -> {
             synchronized (o1) {
                 System.out.println("张三吃醋...");
                 synchronized (o2) {
                     System.out.println("张三要吃醋的同时吃蒜...");
                 }
             }
        });
    }
    public void Thread2() {
        Thread thread = new Thread(() -> {
            synchronized (o2) {
                System.out.println("李四吃蒜...");
                synchronized (o1) {
                    System.out.println("李四要吃蒜的同时吃醋...");
                }
            }
        });
    }
}
